import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import store from '@/store'


const Home = () => import('@/views/home/Home')
const Type = () => import('@/views/type/Type')
const Cart = () => import('@/views/cart/Cart')
const Discover = () => import('@/views/discover/Discover')
const My = () => import('@/views/user/My')
const Login = () => import('@/views/user/Login')
const Regist = () => import('@/views/user/Regist')


const router = new VueRouter({
    mode: 'history',
    routes: [
        { path: '/', component: Home },
        { path: '/type', component: Type },
        { path: '/cart', component: Cart, meta: { isAuth: true } },
        { path: '/discover', component: Discover },
        { path: '/my', component: My, meta: { isAuth: true } },
        { path: '/login', component: Login },
        { path: '/regist', component: Regist },
    ]
})

router.beforeEach((to, from, next) => {
    const { isAuth } = to.meta
    // 假设的一个登录标识
    const isLogin = store.state.user.token
    console.log(typeof (isLogin));
    console.log(store)
    if (isAuth) {
        // 需要登录，才能过去
        if (isLogin) next()
        else location.href = '/login'
    } else {
        next()
    }
})

export default router